import 'dart:convert';
import 'package:du_application/http/HttpUtil.dart';
import 'package:du_application/http/Options.dart';
import 'package:du_application/model/GoodsModel.dart';
import 'package:du_application/model/ResultModel.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class GoodsProvider with ChangeNotifier {
  GoodsModel goodsModel;
  ProductList product;
    Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  getGoods(String goodsId) async{
  goodsModel=null;
     await _prefs.then((ins) async{
    Options options =  Options(ins.getString("token")??"");
 await HttpUtil.getInstance().get("/goods/detail",data:{"id":goodsId},options:options.options).then((data) {
     goodsModel = GoodsModel.fromJson(json.decode(data.toString()));
     if(goodsModel.data.productList.length>0)
     product=goodsModel.data.productList[0];
     else{
          product=ProductList();
     }
  
    notifyListeners();
    });
     });
  }
  selectProduct(id) async{
  await goodsModel.data.productList.forEach((ProductList item){
  if(item.id==id){
    product=item;
  }
});
 notifyListeners();
  }


  collet({int type=0}) async {
 await _prefs.then((ins) async{
   Options options =  Options(ins.getString("token")??"");
  await HttpUtil.getInstance()
        .post("/collect/addordelete",data: {"valueId":goodsModel.data.info.id,"type":type},
           options: options.options)
        .then((res) async {
       ResultModel resultModel=ResultModel.fromJson(json.decode(res));
      if(resultModel.errno==0){
        if(goodsModel.data.userHasCollect==0){
          goodsModel.data.userHasCollect=1;
        }else{
goodsModel.data.userHasCollect=0;
        }
   
    notifyListeners(); 
    }
  });
 });
  }
}
